home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / simula / books / books.lha / kirkerud / profile.sim < prev    next >
Text File  |  1993-08-16  |  5KB  |  150 lines

  1. % ****************************************************************
  2. % *                                                              *
  3. % *  This is the program constructed in chapter 11 of             *
  4. % *  Object Oriented Programming with Simula by Bj|rn Kirkerud;  *
  5. % *                                                              *
  6. % ****************************************************************
  7.  
  8. begin
  9.  
  10.  
  11. !  Data structure:   ;
  12.  
  13.   integer array small_word_count(1 : 20);
  14.   text array    the_small_words(1 : 20);
  15.   integer       number_of_small_words;
  16.  
  17. !  Data invariants:   ;
  18.  
  19.   ! 1: 1 <= number_of_small_words <= 20 ;
  20.   ! 2: For small_word_number = 1, 2, ,  number_of_small_words:
  21.   !    small_word_count(small_word_number) is equal to the number of times
  22.   !    the_small_words(small_word_number) has been read from file;
  23.  
  24.  
  25. procedure Enter_small_words;
  26.     !  Lets the user enter the small words    ;
  27.   begin text word;
  28.     number_of_small_words := 0;
  29.     word :- prompt_for_text("Enter the first small word> ", 20);
  30.     while word =/= notext  do
  31.       begin
  32.         number_of_small_words := number_of_small_words + 1;
  33.         the_small_words(number_of_small_words) :- word;
  34.         small_word_count(number_of_small_words) := 0;
  35.         word :- if number_of_small_words = 20
  36.                 then notext
  37.                 else prompt_for_text("Enter another small word> ", 20);
  38.       end;
  39.   end of Enter_small_words;
  40.  
  41.  
  42. procedure Read_words;
  43.     ! Reads all words from file and counts occurrencies of the small words;
  44.   begin text word, word_buffer;   character c;  integer small_word_number;
  45.     inspect new  infile(prompt_for_text("Name of the file? ", 30)) do
  46.       begin
  47.       !  Generate a text object which is used to build  words:   ;
  48.         word_buffer :- blanks(50);
  49.         open(blanks(80));
  50.         while not lastitem do
  51.           begin
  52.             c := inchar;
  53.             if letter(c) then
  54.               begin ! c  must be the start of a word;
  55.                 word_buffer := notext; word_buffer.setpos(1);
  56.                 while letter(c) do
  57.                   begin
  58.                     if word_buffer.more then  word_buffer.putchar(c);
  59.                     c := inchar;
  60.                   end;
  61.                 word :- word_buffer.strip;
  62.                 for small_word_number := 1 step 1  until number_of_small_words
  63.                 do  if the_small_words(small_word_number) = word
  64.                    then small_word_count(small_word_number)
  65.                          := small_word_count(small_word_number)  + 1;
  66.               end of letter(c);
  67.           end of while not lastitem;
  68.         close;
  69.       end of inspect infile;
  70.     end of Read_words;
  71.  
  72.  
  73. procedure Write_table;
  74.     !  Writes a table which shows how many times each of the small  words
  75.     !  occurs in the file:   ;
  76.   begin integer small_word_length, small_word_number;
  77.   !  First find the length of the longest small word:   ;
  78.     small_word_length := 0;
  79.     for small_word_number := 1 step 1 until  number_of_small_words do
  80.       small_word_length := max(small_word_length,
  81.                          the_small_words(small_word_number).length);
  82.   !  Then write the table:   ;
  83.     outimage;
  84.     for small_word_number := 1 step 1 until  number_of_small_words do
  85.       begin
  86.         outtext("The word """ & the_small_words(small_word_number) & """");
  87.         setpos(13 + small_word_length); outtext("occurs");
  88.         outint(small_word_count(small_word_number), 4);  outtext(" times.");
  89.         outimage;
  90.       end;
  91.     outimage;
  92.   end of Write_table;
  93.  
  94. procedure Write_histogram;
  95.     !  Write an histogram which shows the relative frequencies  
  96.     !  of the small words:   ;
  97.   begin
  98.     integer count_sum, small_word_number, small_word_length,
  99.             asterisk_number, max_asterisks;
  100.      real   percentage;
  101.   !  First count the total number of small words  in the file:    ;
  102.     count_sum := 0; small_word_length := 0;
  103.     for small_word_number := 1 step 1 until  number_of_small_words do
  104.       begin
  105.         count_sum := count_sum +  small_word_count(small_word_number);
  106.         small_word_length := max(small_word_length,
  107.             the_small_words(small_word_number).length);
  108.       end;
  109.   !  Then write the histogram:   ;
  110.     if count_sum > 0 then
  111.     begin
  112.       max_asterisks := 0;
  113.       for small_word_number := 1 step 1 until  number_of_small_words do
  114.         max_asterisks := max(max_asterisks,
  115.             100 * small_word_count(small_word_number)  / count_sum/2);
  116.       for small_word_number := 1 step 1 until  number_of_small_words do
  117.         begin
  118.           outtext(the_small_words(small_word_number) & ":");
  119.           setpos(small_word_length + 3);
  120.           percentage := 100 * small_word_count(small_word_number)  / count_sum;
  121.           for asterisk_number := 1 step 1 until percentage/2 do outchar('*');
  122.           setpos(small_word_length + 4 + max_asterisks);
  123.           outfix(percentage, 1, 5);
  124.           outtext(" of the small words");
  125.           outimage;
  126.         end;
  127.       setpos(small_word_length + 3);  outtext("0   10  20  30  40");
  128.       outimage; outimage;
  129.     end;
  130.   end of Write_histogram;
  131.  
  132.  
  133. !   An auxiliary procedure:    ;
  134. text procedure prompt_for_text(prompt, max_text_length);
  135.     text prompt; integer max_text_length;
  136.    begin
  137.      outtext(prompt); breakoutimage; inimage;
  138.      prompt_for_text :- intext(max_text_length).strip;
  139.    end;
  140.  
  141.  
  142. !  Imperatives:   ;
  143.  
  144.   Enter_small_words;
  145.   Read_words;
  146.   Write_table;
  147.   Write_histogram;
  148.  
  149. end
  150.